/**
 * Created by taohj on 2014/8/15.
 */
var _data = new Object();


/**
 * 模块列表
 * */
_data.initgrid = function () {

    _data.datagrid = $("#admin_data_list_datagrid");


    _data.datagrid.datagrid({
        url: '/admin/data/list',
        singleSelect: true,
        collapsible: true,
        pagination: true,
        rownumbers: true,
        fitColumns: true,
        sortName: "id",
        sortOrder: "desc",
        fit: true,
        checkOnSelect: false,
        selectOnCheck: false,
        pageList: [20, 50, 100, 200],
        pageSize: 20,
        toolbar: [
            {
                iconCls: 'icon-add',
                text: '新增',
                handler: function () {
                    var createDialog = $("<div></div>");
                    createDialog.dialog({
                        title: '新增',
                        href: '/admin/data/createV',
                        modal: true,
                        width: 450,
                        top: 150
                    });
                }
            },
            '-',
            {
                iconCls: 'icon-edit',
                text: '更新',
                handler: function () {
                    var selectRow = _data.datagrid.datagrid("getSelected");
                    if (!selectRow) {
                        //todo
                        return;
                    }

                    var id = selectRow.id;
                    var createDialog = $("<div></div>");
                    createDialog.dialog({
                        title: '更新',
                        href: '/admin/data/modifyV' + '?id=' + id,
                        modal: true,
                        width: 400,
                        top: 150
                    });

                }
            },
            '-',
            {
                iconCls: 'icon-remove',
                text: '删除',
                handler: function () {

                    var selectRow = _data.datagrid.datagrid("getSelected");
                    if (!selectRow) {
                        //todo
                        return;
                    }

                    $.messager.confirm('删除确认', '确定要删除此项么？ ', function (r) {
                        if (r) {
                            $.post('/admin/data/remove', {
                                id: selectRow.id
                            }, function (result) {
                                util.requestUtil(result, null, _data.datagrid);
                            });
                        }
                    });
                }
            },
            '-',
            {
                iconCls: 'icon-remove',
                text: '通过',
                handler: function () {
                    var datas = new Array();
                    var checkeds = _data.datagrid.datagrid('getChecked');
                    for (var i = 0; i < checkeds.length; i++) {
                        var row = checkeds[i];
                        datas[i] = row.id;
                    }
                    $.post("admin/data/audit", { 'datas[]': datas, auditStatus: 183}, function (result) {
                        util.requestUtil(result, null, _data.datagrid)
                    });
                }
            },
            '-',
            {
                iconCls: 'icon-remove',
                text: '不通过',
                handler: function () {
                    var datas = new Array();
                    var checkeds = _data.datagrid.datagrid('getChecked');
                    for (var i = 0; i < checkeds.length; i++) {
                        var row = checkeds[i];
                        datas[i] = row.id;
                    }
                    $.post("admin/data/audit", { 'datas[]': datas, auditStatus: 182}, function (result) {
                        util.requestUtil(result, null, _data.datagrid)
                    });
                }
            }
        ],
        columns: [
            [
                {field: "id_ck", width: 60, checkbox: true, formatter: function (value, row, index) {
                    return row.id;
                }},
                {field: "id", title: "编号", width: 80, sortable: true},
                {field: "title", title: " 标题", width: 200, sortable: true},
                {field: "tags", title: "标签", width: 80, sortable: true},
                {field: "license", title: "授权信息", width: 200, sortable: true, },
                {field: "storage", title: "存储形式", width: 200, sortable: true, formatter: function (value) {
                    return value ? value.name : '';
                }},
                {field: "url", title: "地址", width: 200, sortable: true, formatter: function (value) {
                    return value ? '<a href="' + value + '" target="_blank">预览</a>' : '';
                }},
                {field: "fileFormat", title: "文件格式", width: 200, sortable: true},
                {field: "visibility", title: "是否公开", width: 200, sortable: true, formatter: function (value, row, index) {
                    return value ? value.name : '';

                }},
                /* {field: "status", title: "状态", width: 200, sortable: true, formatter: function (value, row, index) {
                 return value ? value.name : '';

                 }},*/
                {field: "auditStatus", title: "审批状态", width: 200, sortable: true, formatter: function (value, row, index) {
                    return value ? value.name : '';
                }},
                {field: "version", title: "当前版本", width: 200, sortable: true},
                {field: "GUID", title: "唯一标识列", width: 200, sortable: true},
                {field: "creater", title: "创建人", width: 200, sortable: true, formatter: function (value) {
                    return value ? value.username : '';
                }},
                {field: "createTime", title: "创建时间"},
                {field: "dataSet", title: "数据集", formatter: function (value) {
                    return value ? value.name : '';
                }},
                {field: "org", title: "机构", formatter: function (value) {
                    return value ? value.name : '';
                }},
                {field: "updater", title: "最后修改人", formatter: function (value) {
                    return value ? value.username : '';
                }},
                {field: "updateTime", title: "最后修改时间"}
            ]
        ]
    });
};


//新建提交
_data.createSubmit = function (button) {
    var form = util.form(button);
    alert(form.find('input[name="storage"]').val());
    form.form('submit', {
        url: '/admin/data/create',
        success: function (result) {
            var dialog = $(button).closest("div.window-body");
            util.requestUtil(result, dialog, _data.datagrid)
        }
    });

};


//修改提交
_data.modifySubmit = function (button) {
    var form = util.form(button);
    form.form('submit', {
        url: '/admin/data/modify',
        success: function (result) {
            var dialog = $(button).closest("div.window-body");
            util.requestUtil(result, dialog, _data.datagrid)
        }
    });
};


//搜索
_data.searchSubmit = function (button) {
    var form = util.form(button);

    var params = util.serializeObject(form);
    _data.datagrid.datagrid("load", params);
}

